package org.activequant.core.util;

import java.util.Iterator;

/**
 * Helper to write iterators.<br/>
 * An abstract class, implements the Iterator&lt;T&gt; interface. Holds the following associated variable:
 * <ul>
 * <li>next(T)</li>
 * </ul>
 * The abstract method is:
 * <ul>
 * <li>protected abstract T fetchNext();</li>
 * </ul>
 * @author Mike Kroutikov.
 *
 * @param <T> iterable type.
 */
public abstract class IteratorHelper<T> implements Iterator<T> {
	/**
	 * private T next = null;
	 */
	private T next = null;
	
	protected abstract T fetchNext();

	public boolean hasNext() {
		if(next != null) {
			return true;
		}
		
		next = fetchNext();
		return next != null;
	}

	public T next() {
		if(!hasNext()) {
			return null;
		}
		
		T out = next;
		next = null;
		
		return out;
	}
	/**
	 * throws an UnsupportedOperationException
	 */
	public void remove() {
		throw new UnsupportedOperationException();
	}

}
